home *** CD-ROM | disk | FTP | other *** search
- Subject: v06i017: context - generalized context printer (context)
- Newsgroups: mod.sources
- Approved: rs@mirror.UUCP
-
- Submitted by: genrad!decvax!cwruecmp!ncoast!allbery (Brandon S. Allbery)
- Mod.sources: Volume 6, Issue 17
- Archive-name: context
-
- #!/bin/sh
- # This is a shell archive. Remove anything before this line,
- # then unpack it by saving it in a file and typing "sh file".
- # Contents: README context.c
-
- echo x - README
- sed 's/^XX//' > "README" <<'@//E*O*F README//'
- XXThe following file is my context'' program for displaying the context of
- XXa message of the general form
-
- XX <garbage> filename <garbege> linenumber <garbage>
-
- XXSample usage:
-
- XXScript started on Fri Jun 6 18:29:09 1986
- XX% grep range context.c
- XXint cxtrange = 3;
- XX if ((cxtrange = atol(argv[1])) < 1 || cxtrange > 25)
- XX cxtrange = 3;
- XX lolino = (cxtln < cxtrange - 1? 1: cxtln - cxtrange);
- XX hilino = cxtln + cxtrange;
-
- XX% grep -n range /dev/null context.c | context # /dev/null forces filename
- XX**************
- XX* context.c:6:int cxtrange = 3;
- XX*****
- XX
- XX char curfile[512];
- XX FILE *curfp = NULL;
- XX* int cxtrange = 3;
- XX
- XX char *gets();
- XX
-
- XX**************
- XX* context.c:20: if ((cxtrange = atol(argv[1])) < 1 || cxtrange > 25)
- XX*****
- XX exit(1);
- XX }
- XX if (argc == 2)
- XX* if ((cxtrange = atol(argv[1])) < 1 || cxtrange > 25)
- XX cxtrange = 3;
- XX while (gets(context) != (char *) 0) {
- XX for (cp = context; *cp != '.' && *cp != '/' && *cp != '-' && *cp != '_' && !isalnum(*cp); cp++)
-
- XX**************
- XX* context.c:21: cxtrange = 3;
- XX*****
- XX }
- XX if (argc == 2)
- XX if ((cxtrange = atol(argv[1])) < 1 || cxtrange > 25)
- XX* cxtrange = 3;
- XX while (gets(context) != (char *) 0) {
- XX for (cp = context; *cp != '.' && *cp != '/' && *cp != '-' && *cp != '_' && !isalnum(*cp); cp++)
- XX if (*cp == '\0')
-
- XX**************
- XX* context.c:49: lolino = (cxtln < cxtrange - 1? 1: cxtln - cxtrange);
- XX*****
- XX if (*fcp == '\0')
- XX continue;
- XX cxtln = atol(fcp);
- XX* lolino = (cxtln < cxtrange - 1? 1: cxtln - cxtrange);
- XX hilino = cxtln + cxtrange;
- XX if (lolino < curln) {
- XX fseek(curfp, 0L, 0);
-
- XX**************
- XX* context.c:50: hilino = cxtln + cxtrange;
- XX*****
- XX continue;
- XX cxtln = atol(fcp);
- XX lolino = (cxtln < cxtrange - 1? 1: cxtln - cxtrange);
- XX* hilino = cxtln + cxtrange;
- XX if (lolino < curln) {
- XX fseek(curfp, 0L, 0);
- XX curln = 0;
-
- XX%
- XXscript done on Fri Jun 6 18:29:55 1986
-
- XXAlso useful for examining the causes of C errors (although I've received mixed
- XXreviews on that; seems that BSD4.2 has a smarter'' program callederrors'').
-
- XXSimply compile context.c -- there are no V7/S5/BSD dependencies that I know
- XXof. Then enjoy.
-
- XX --Brandon
- @//E*O*F README//
- chmod u=rw,g=rw,o=rw README
-
- echo x - context.c
- sed 's/^XX//' > "context.c" <<'@//E*O*F context.c//'
- XX#include <stdio.h>
- XX#include <ctype.h>
-
- XXchar curfile[512];
- XXFILE *curfp = NULL;
- XXint cxtrange = 3;
-
- XXchar *gets();
-
- XXmain(argc, argv)
- XXchar **argv; {
- XX char context[512], fline[512], *cp, *fcp;
- XX long cxtln, lolino, hilino, curln;
-
- XX if (argc > 2) {
- XX fprintf(stderr, "Usage: context [nlines] < listfile\n");
- XX exit(1);
- XX }
- XX if (argc == 2)
- XX if ((cxtrange = atol(argv[1])) < 1 || cxtrange > 25)
- XX cxtrange = 3;
- XX while (gets(context) != (char *) 0) {
- XX for (cp = context; *cp != '.' && *cp != '/' && *cp != '-' && *cp != '_' && !isalnum(*cp); cp++)
- XX if (*cp == '\0')
- XX break;
- XX if (*cp == '\0')
- XX continue;
- XX strcpy(fline, cp);
- XX for (fcp = cp, cp = fline; *cp == '_' || *cp == '.' || *cp == '/' || *cp == '-' || isalnum(*cp); cp++, fcp++)
- XX ;
- XX if (*cp == '\0')
- XX continue;
- XX *cp = '\0';
- XX if (curfp == (FILE *) 0 || strcmp(curfile, fline) != 0) {
- XX if (curfp != (FILE *) 0)
- XX fclose(curfp);
- XX if ((curfp = fopen(fline, "r")) == (FILE *) 0) {
- XX perror(fline);
- XX continue;
- XX }
- XX strcpy(curfile, fline);
- XX curln = 0;
- XX }
- XX for (; *fcp != '\0' && !isdigit(*fcp); fcp++)
- XX ;
- XX if (*fcp == '\0')
- XX continue;
- XX cxtln = atol(fcp);
- XX lolino = (cxtln < cxtrange - 1? 1: cxtln - cxtrange);
- XX hilino = cxtln + cxtrange;
- XX if (lolino < curln) {
- XX fseek(curfp, 0L, 0);
- XX curln = 0;
- XX }
- XX if (cxtln == curln) { /* already shown */
- XX printf("*****\n* %s\n*****\n\n", context);
- XX continue;
- XX }
- XX printf("**************\n* %s\n*****\n", context);
- XX while (fgets(fline, 512, curfp) != (char *) 0 && ++curln < lolino)
- XX ;
- XX if (curln < lolino)
- XX continue;
- XX out(fline, curln == cxtln);
- XX while (fgets(fline, 512, curfp) != (char *) 0 && ++curln <= hilino)
- XX out(fline, curln == cxtln);
- XX putchar('\n');
- XX }
- XX}
-
- XXout(s, flg)
- XXchar *s; {
- XX printf("%c %s", (flg? '*': ' '), s);
- XX}
- @//E*O*F context.c//
- chmod u=rw,g=rw,o=rw context.c
-
- echo Inspecting for damage in transit...
- temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
- trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
- cat > $temp <<\!!!
- 81 362 2154 README
- 74 291 1772 context.c
- 155 653 3926 total
- !!!
- wc README context.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
- if test -s $dtemp
- then echo "Ouch [diff of wc output]:" ; cat $dtemp
- else echo "No problems found."
- fi
- exit 0
-